package vcsn.handlers;


import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;

import vcsn.ui.ConfigurationDialog;

public class ConfigurationHnadler extends AbstractHandler{

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
	    if (window != null){
	        IStructuredSelection selection = (IStructuredSelection) window.getSelectionService().getSelection();
	        Object firstElement = selection.getFirstElement();
	        if (firstElement instanceof IAdaptable){
	            IProject project = (IProject)((IAdaptable)firstElement).getAdapter(IProject.class);
	            try {
	            	ConfigurationDialog dialog = new ConfigurationDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.NONE, project);
	        		dialog.open();
				} catch (Exception e) {
					e.printStackTrace();
				}
	        }
	        else{ 
	        	window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
	    		MessageDialog.openInformation(window.getShell(), "Error", "No project is selected!Please select a project.");
	    		return null;
		    }
	    }else{
	    	window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
    		MessageDialog.openInformation(window.getShell(), "Error", "Window is null!");
	    }

		return null;
	}

}
